﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace EIS
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainFormLoad(object sender, EventArgs e)
        {
            SqlServerHelper serverHelper = new SqlServerHelper();
            List<string> tables = serverHelper.GetTables();
            tableComboBox.DataSource = tables;
        }

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern bool DeleteFile(string path);

        private void ExportButtonClick(object sender, EventArgs e)
        {
            if (tableComboBox.SelectedValue != null)
            {
                SqlServerHelper serverHelper = new SqlServerHelper();
                string result = serverHelper.ExportDataTable(tableComboBox.SelectedValue.ToString(),ScriptTextBox.Text.Trim());

                File.WriteAllText(tableComboBox.SelectedValue + ".sql", result);

                MessageBox.Show("OK");
            }
        }

        private void EntityButtonClick(object sender, EventArgs e)
        {
            FileDialog dialog = new OpenFileDialog();
            dialog.Filter = "dll|*.dll||";

            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                assemblyTextBox.Text = dialog.FileName;
                LoadAssembly(dialog.FileName);
            }
        }

        private void LoadAssembly(string filePath)
        {
            Assembly assembly = Assembly.LoadFile(filePath);
            Type[] type = assembly.GetTypes();

            classComboBox.DataSource = type;
            classComboBox.DisplayMember = "FullName";
        }

        private void ClassComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            Type type = classComboBox.SelectedItem as Type;

            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendLine(string.Format("{0} aa = new {0}();", type.Name));

            PropertyInfo[] propertyInfos = type.GetProperties();

            foreach (var propertyInfo in propertyInfos)
            {
                stringBuilder.AppendLine(string.Format("aa.{0} = To<{1}>(dataRow,\"{0}\");", propertyInfo.Name, propertyInfo.PropertyType.Name));
            }

            entityTextBox.Text = stringBuilder.ToString();
        }

        private void TableComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            string tableName= tableComboBox.SelectedValue.ToString();
            if(!string.IsNullOrEmpty(tableName))
            {
                List<string> columns = SqlServerHelper.GetColumns(tableName);
                ColumnCheckedListBox.DataSource = columns;
                for(int i=0;i<ColumnCheckedListBox.Items.Count;i++)
                    ColumnCheckedListBox.SetItemChecked(i,true);
            }
        }
    }
}